/**
 * Created by python on 19-3-4.
 */
var vm = new Vue({
    el: '#app',
    data: {
        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        avatar_url:sessionStorage.avatar_url || localStorage.avatar_url,
        labelList:[],
    },
    mounted:function () {
        axios.get(HOST + '/labels/', {
            headers: {
                        'Authorization': 'JWT ' + this.token
                    },
            responseType: 'json'
        })
            .then(response => {
                this.labelList = response.data

            })
            .catch(error => {
                console.log(error.response.data)
            });
    },
    methods:{
        on_attention:function (label_id,is_attention) {
            if (is_attention){
                alert('用户已关注')
                return
            }
            if (this.user_id && this.token ){
                axios.post(HOST + '/labels/attention/', {
                    label_id:label_id

                },{
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    }, responseType: 'json'
                    })
            .then(response => {
                alert('关注成功')
                for(var i=0;i<this.labelList.length;i++){
                    if (this.labelList[i].id == label_id){
                                    this.labelList[i].is_attention = true;
                                    this.labelList[i].attention += 1;
                                    return
                                }

            }
            })
            .catch(error => {
                alert('发布失败')
                console.log(error.response)
            });
            }else {
                alert('请登录后再关注')
                location.href = '/person-login.html?next=/qa-allTag.html'
            }



        }
    },
    filters:{
      attention_status:function (value) {
          if (value){
              return '已关注'
          }else {
              return '加关注'
          }
      }
    }

})